const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    lists:[],
    navHeight:'',
    isloading:true,
    infos:[]
  },
  onLoad(options) {
    this.setData({
      navHeight: app.globalData.navHeight
    })
    this.collectInfoList()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onShow(){
    
  },
  onReachBottom(){

  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    
  },

  collectInfoList(){
    Controller.collectInfoList().then(res=>{
      if(res.data.baseData && res.data.baseData.length>0){
        for(var item of res.data.baseData){
            item.select=false
        }
        this.setData({
          lists:res.data.baseData
        })
      }
    })
  },
  select(e){
    var name=e.currentTarget.dataset.name
    var infos=[];
    var id=e.currentTarget.dataset.id;
    var lists=this.data.lists;
    for(var item of lists){
      if(id==item.id){
        item.select=!item.select
      }
    }
    for(var i=0;i<lists.length;i++){
      if(lists[i].select){
        infos.push(lists[i].title)
      }
    }
    this.setData({
      infos,
      lists
    })
  },
  save(){
    if(this.data.infos.length<=0){
      wx.showToast({
        title:"请选择你感兴趣的内容",
        icon:"none"
      })
      return
    }
    
    var data={
      infos:this.data.infos.join(',')
    }
    Controller.collectInfo(data).then(res=>{
      wx.showToast({
        title:"提交成功",
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.switchTab({
          url:"/pages/Index/Index"
        });
      },2000)
    })
  },
  prev(){
    wx.switchTab({
      url:"/pages/Index/Index"
    });

  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },

})